# -*- shell-script -*-

# 00minimal - Routines for lsvpd output.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003, 2004

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 00minimal,v 1.1 2006/04/11 18:38:28 emunson Exp $

true || return 0

######################################################################

usage () {
    cat <<EOF 1>&2
usage: $0 [options]
options: -h      print this usage message
         -m      Mark VPD as global or partition-private.   
         -s str  Use str as serial number.
         -t str  Use str as type/model number.
	 -d db   Directory db contains database, instead of default.
	 -z tgz  File tgz contains database archive (overrides -d).
EOF
    exit 1
}

process_options ()
{
    # Defaults.
    mark_vpd=false
    serial_num=""
    type_model=""

    options=":hms:t:${db_path_option}:z:"
    while getopts ${options} opt "$@" ; do
	case ${opt} in
	    (h) usage                ;;
	    (m) mark_vpd=true        ;;
	    (s) serial_num="$OPTARG" ;;
	    (t) type_model="$OPTARG" ;;
	    (${db_path_option}|z) :  ;; # Done by process_db_path_options().
	    \?)
                echo "unknown option \`${OPTARG}'" 1>&2
		echo 1>&2
		usage
		;;
	esac
    done

    shift $((${OPTIND} - 1))
    [ -n "$1" ] && usage
    return 0
}

######################################################################

print_global_header ()
{
    [ -n "$serial_num" ] || serial_num=$(get_serial)
    [ -n "$type_model" ] || type_model=$(get_model)

    local os_info="unknown"

    if [ -f "${db_uname_dir}/kernel-name" -a \
	-f "${db_uname_dir}/kernel-release" ] ; then

	local name release
	read name < "${db_uname_dir}/kernel-name"
	read release < "${db_uname_dir}/kernel-release"

	os_info="${name} ${release}"
    fi

    printf "*VC 5.0\n*TM %s\n*SE %s\n*PI %s\n*OS %s\n" \
	"$type_model" "$serial_num" "$serial_num" "$os_info"
}

render_linux_vpd ()
{
    local x="$1"

    if [ -d "$x" ] ; then
	lsvpd_render_linux_vpd_directory "$x"
    else
	lsvpd_render_linux_vpd_file "$x"
    fi
}

lsvpd_render_linux_vpd_directory ()
{
    local d="$1"

    local node="${d%/linux,vpd}"

    local vpd_subdirs
    vpd_subdirs_list_hook "$node"
    local vpd_dir
    for vpd_dir in $vpd_subdirs ; do
	local vpd_value
	vpd_field_get "$vpd_dir" "YL"
	local yl="$vpd_value"
	should_render "$node" "" "" "$yl" || continue

	local vpd_fields
	vpd_fields_list_hook "$vpd_dir"

	local k
	for k in $vpd_fields ; do
	    vpd_field_get "$vpd_dir" "$k"
	    case "${k%.*}" in
		(FC) $mark_vpd || vpd_value="????????" ;;
		(U?) vpd_value="" ;; # Suppress.  Used internally for lsmcode.
	    esac
	    [ -n "$vpd_value" ] && echo "*${k%.*} ${vpd_value}"
	done
    done
}

lsvpd_render_linux_vpd_file ()
{
    local f="$1"

    if $mark_vpd ; then
	cat "$f"
    else
	sed -e 's/^\*FC .*/\*FC ????????/' "$f"
    fi
}

should_render ()
{
    local dir="$1"
    local ds="$2"
    local ax="$3"
    local yl="$4"

    should_render_basic "$dir" "$ds" "$ax" "$yl"
}

should_render_basic ()
{
    local dir="$1"
    local ds="$2"
    local ax="$3"
    local yl="$4"

    # Don't render logicals.
    [ -f "${dir}/lsvpd,logical" ] && return 1

    return 0
}
